package name.ekr.edu.threads.chatserver.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import name.ekr.edu.threads.chatserver.CommunicationError;
import name.ekr.edu.threads.chatserver.ConnectionError;
import name.ekr.edu.threads.chatserver.Server;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static name.ekr.edu.threads.chatserver.Constants.*;


public class Launcher {
	static Logger logger = LoggerFactory.getLogger(Launcher.class);

	public static void main(String... args) throws ConnectionError, CommunicationError, InterruptedException {
		try(	BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in, ENCODING));
			PrintWriter consoleWriter = new PrintWriter(new OutputStreamWriter(System.out, ENCODING), true);
			Server server = new ServerProxy(new Connector(HOST, PORT))) {
				
				WriterClient writerClient = new WriterClient(server, consoleWriter);
				ReaderClient readerClient = new ReaderClient(server, consoleReader);
				writerClient.start();
				readerClient.start();
				
				readerClient.join();
			
		} catch (IOException e) {
			logger.error("", e);
			throw new RuntimeException("Can't read or write to console", e);
		} 
	}
}
